define(['template/phoneSetting/indexTpl','common','wx','wx.ajax','wx.tpl','wx.validator','vendor/jquery.dataTables'],function(tpl,common,wx){
    var phoneSetting = {};
    var isWrite;
    var currentType=0;
    function init(id,w){
        isWrite = w|0;
        var t = new Date().getTime();
        $.when(
            $.ajax('/web/res/css/layout.css?_='+t),
            $.ajax('/web/res/css/phoneSetting.css?_='+t),
            $.ajax('/web/res/css/jquery.dataTables.min.css')
        ).done(function(layout,tableCss,phoneCss){
            $('<style type="text/css">').append(layout[0],tableCss[0],phoneCss[0]).appendTo('head');
            $('#window-frame-phoneSetting').html(tpl.container);
            $('#Js-phoneSetting-cate').delegate('li','click',function(){
                $(this).addClass('active').siblings('.active').removeClass('active');
                if($(this).data('id') == '-1')
                    help();
                else
                    action($(this).data('id'));
            });
            $('#Js-phoneSetting-cate').find('[data-id="0"]').trigger('click');

        });
	}
	
    function action(type){
        currentType = type|0;
        var tipTpl = '<strong>描述：</strong><%description%>\
            <br><strong>修改：</strong><%modify_by%>&nbsp;&nbsp;<%modify_time%>\
            <br><strong>创建：</strong><%create_by%>&nbsp;&nbsp;<%create_time%>';

        wx.sendData('/phone/get','type='+type,function(data){
            $('#Js-phoneSetting-body').html(tpl.settingTable);
            for(var i=0;i<data.result.length;i++){
                if(isWrite){
                    if(currentType == 0){
                        data.result[i].ctrl='<a class="Js-app-update app-version-action" href="javascript:;" data-ver="'+data.result[i].value+'" data-id="'+data.result[i].id+'">更新版本</a>';
                    }else{
                        data.result[i].ctrl='<a class="Js-app-modify app-version-action" href="javascript:;" data-id="'+i+'">修改</a>';
                    }
                    data.result[i].ctrl+='<a class="Js-app-changeStatus app-version-action" href="javascript:;" data-status="'+data.result[i].status+'" data-id="'+data.result[i].id+'">更改状态</a>';
                } else{
                    data.result[i].ctrl = '';
                }

                if(data.result[i].status == 1){
                    data.result[i].status = '显示';
                } else {
                    data.result[i].status = '隐藏';
                }
            }

            $('#Js-setting-table').DataTable({
                data:data.result,
                columns: [
                    { data: 'name' },
                    { data: 'value' },
                    { data: 'status' },
                    { data: 'ctrl' }
                ],
                aoColumnDefs: [
                        {   
                            aTargets: [0,1,2],
                            mRender: function (content, type, full) {
                                return '<div class="Js-tip" data-placement="bottom" data-content="'+wx.tpl(tipTpl,full)+'">'+content+'</div>';
                        },
                    }
                ],
                language:common.dataTableText
            });
            $('.Js-tip').popover({trigger:'hover',html:true,template:'<div class="popover" role="tooltip"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div></div>'});
            $('#Js-setting-table_length').append(tpl.versionAdd);
            $('#Js-version-add').click(addAndEditAction);
            $('.Js-app-update').click(updateAction);
            $('.Js-app-changeStatus').click(changeStatus);
            $('.Js-app-modify').click(function(){
                addAndEditAction(null,data.result[$(this).data('id')|0]);
            });

        });
    }

    function updateAction(){
        var _this = this;
        var ver = $(this).data('ver');
        var id = $(this).data('id');
        ver++;
        wx.sendData('/phone/update','id='+id+'&value='+ver+'&type='+currentType,function(data){
            $(_this).parents('tr').find('td:eq(1)').text(ver);
            $(_this).data('ver',ver);
        });
    }

    function changeStatus(){
        var _this = this;
        var status = $(this).data('status');
        var id = $(this).data('id');
        status = status=='1'?'0':'1';

        wx.sendData('/phone/update','id='+id+'&action=status&value='+status,function(data){
            $(_this).parents('tr').find('td:eq(2)').text(status=='1'?'显示':'隐藏');
            $(_this).data('status',status);
        });
    }

    function addAndEditAction(event,data){
        data = data ||{};
        data.type = currentType;
        $('#Js-phoneSetting-body').html(wx.tpl(tpl.versionAddContent,data));
        wx.validator();
        $('.app-version-addform').on('validator',function(){
            $('#Js-addBack').trigger('click');
        });
        $('#Js-addBack').click(function(){
            action(currentType);
        });
    }

    function help(){
        $('#Js-phoneSetting-body').html(tpl.help);
    }

    phoneSetting.init = init;
    return phoneSetting;
});